const genPromise = (data) => {
  return Promise.resolve(data);
};

const task = function* () {
  const r1 = yield genPromise(111);
  const r2 = yield genPromise(222);
  console.log(r1);
  console.log(r2);
};

function co(gen) {
  const ctx = this;
  return new Promise((resolve, reject) => {
    if (typeof gen === 'function') gen = gen.call(ctx);
    if (!gen || typeof gen.next !== 'function') return resolve(gen);
    // 自执行一次next函数
    const next = (data) => {
      try {
        const result = gen.next(data);
        // console.log(result);
        if (result.done) resolve(result.value);
        result.value.then((data) => {
          next(data);
        });
      } catch (error) {
        reject(error);
      }
    };
    next();
  });
}

const run = () => {
  const gen = task();
  /* gen.next().value.then((data) => {
    gen.next(data).value.then((data) => {
      gen.next(data);
    });
  }); */
  co(task);
};

run();
